package com.example.videoplayer;

import org.json.JSONObject;

import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;

public class NetworkTask {

    public static JSONObject sendPostRequest(String requestURL, String postBody) {
        HttpURLConnection connection = null;
        BufferedReader reader = null;

        try {
            URL url = new URL(requestURL);
            connection = (HttpURLConnection) url.openConnection();
            connection.setRequestMethod("POST");
            connection.setRequestProperty("Content-Type", "application/json");
            connection.setUseCaches(false);
            connection.setDoInput(true);
            connection.setDoOutput(true);
            if (postBody != null) {
                DataOutputStream wr = new DataOutputStream(connection.getOutputStream());
                wr.writeBytes(postBody);
                wr.flush();
                wr.close();
            }

            // Start communicating with the server
            connection.connect();

            int responseCode = connection.getResponseCode();
            if (responseCode == HttpURLConnection.HTTP_OK) {
                InputStream inputStream = connection.getInputStream();
                reader = new BufferedReader(new InputStreamReader(inputStream));
                StringBuilder bufferedStr = new StringBuilder();
                String line = null;
                while ((line = reader.readLine()) != null) {
                    bufferedStr.append(line + "\n");
                }
                JSONObject jsonObject = new JSONObject(bufferedStr.toString());
                return jsonObject;
            } else {
                // Error handling
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (connection != null) {
                connection.disconnect();
            }
            try {
                if (reader != null) {
                    reader.close();
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return null;
    }
    public static JSONObject sendPutRequest(String requestURL, String postBody) {
        HttpURLConnection connection = null;
        BufferedReader reader = null;

        try {
            URL url = new URL(requestURL);
            connection = (HttpURLConnection) url.openConnection();
            connection.setRequestMethod("PUT");
            connection.setRequestProperty("Content-Type", "application/json");
            connection.setUseCaches(false);
            connection.setDoInput(true);
            connection.setDoOutput(true);

            // Set request body if necessary
            if (postBody != null) {
                DataOutputStream wr = new DataOutputStream(connection.getOutputStream());
                wr.writeBytes(postBody);
                wr.flush();
                wr.close();
            }

            // Start communicating with the server
            connection.connect();
            int responseCode = connection.getResponseCode();
            if (responseCode == HttpURLConnection.HTTP_OK) {
                InputStream inputStream = connection.getInputStream();
                reader = new BufferedReader(new InputStreamReader(inputStream));
                StringBuilder bufferedStr = new StringBuilder();
                String line = null;
                while ((line = reader.readLine()) != null) {
                    bufferedStr.append(line + "\n");
                }
                JSONObject jsonObject = new JSONObject(bufferedStr.toString());
                return jsonObject;
            } else {
                // Error handling
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (connection != null) {
                connection.disconnect();
            }
            try {
                if (reader != null) {
                    reader.close();
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return null;
    }
    public static JSONObject sendGetRequest(String requestURL) {
        HttpURLConnection connection = null;
        BufferedReader reader = null;

        try {
            URL url = new URL(requestURL);
            connection = (HttpURLConnection) url.openConnection();
            connection.setRequestMethod("GET");

            // Start communicating with the server
            connection.connect();

            int responseCode = connection.getResponseCode();
            if (responseCode == HttpURLConnection.HTTP_OK) {
                InputStream inputStream = connection.getInputStream();
                reader = new BufferedReader(new InputStreamReader(inputStream));
                StringBuilder bufferedStr = new StringBuilder();
                String line = null;
                while ((line = reader.readLine()) != null) {
                    bufferedStr.append(line + "\n");
                }
                JSONObject jsonObject = new JSONObject(bufferedStr.toString());
                return jsonObject;
            } else {
                // Error handling
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (connection != null) {
                connection.disconnect();
            }
            try {
                if (reader != null) {
                    reader.close();
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return null;
    }
}